package com.base.myproject.client.ui;

import com.base.myproject.client.HR.Salary.AddExtSalary;
import com.base.myproject.client.HR.Salary.Hr_helper;
import com.base.myproject.client.HR.Salary.MakeSalaryX;
import com.base.myproject.client.HR.Salary.Salary;
import com.base.myproject.client.HR.Salary.Salary_Bonus;
import com.base.myproject.client.HR.Salary.Salary_Bonus_ploy;
import com.base.myproject.client.HR.Salary.Salary_Ploy;
import com.base.myproject.client.HR.setting.Salary_Item;
import com.base.myproject.client.HR.setting.Social_S;
import com.base.myproject.client.HR.setting.Social_base;
import com.base.myproject.client.busi.BaseTreeStore;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelIconProvider;
import com.extjs.gxt.ui.client.data.TreeModel;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanelSelectionModel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/**
 * @author linlepeng@gmail.com
 * 树形菜单
 *
 */
public class BaseTreeMenu extends BaseJDBCPanel{
	
	static BaseTreeMenu basetreemenu = null;
	TreePanel<TreeNodeModelData> treeSalray;
	TreePanel<TreeNodeModelData> treeEmploy;
	BaseModelIconProvider<TreeNodeModelData> bmip = new BaseModelIconProvider<TreeNodeModelData>();

	public BaseTreeMenu(){
		init();
	}
	
	public static BaseTreeMenu getInstance(){
		if(basetreemenu ==null){
			basetreemenu = new BaseTreeMenu();
		}
		return basetreemenu;
	}
	
	public void init(){
		initWidget();
		treeSalray.addListener(Events.OnClick, new Listener<TreePanelEvent<TreeModel>>(){
			@Override
			public void handleEvent(TreePanelEvent<TreeModel> be) {
				if (be.getItem().getChildCount() == 0) {
					String command = be.getItem().get("command");
					if(command.equals("Salary")){
						Salary w = new Salary();
						w.complete();
						BaseTabPanel.getInstance().addTab("薪金列表", w);
					}else if(command.equals("MakeSalaryX")){
						MakeSalaryX w = new MakeSalaryX();
						w.complete();
						BaseTabPanel.getInstance().addTab("产生薪金", w);
					}else if(command.equals("AddExtSalary")){
						AddExtSalary w = new AddExtSalary();
						w.complete();
						BaseTabPanel.getInstance().addTab("津贴", w);
					}else if(command.equals("Salary_Item")){
						Salary_Item w = new Salary_Item();
						w.complete();
						BaseTabPanel.getInstance().addTab("薪金项目设置", w);
					}else if(command.equals("Salary_Bonus")){
						Salary_Bonus w = new Salary_Bonus();
						w.complete();
						BaseTabPanel.getInstance().addTab("添加花红",w);
					}else if(command.equals("Salary_Bonus_ploy")){
						Salary_Bonus_ploy w = new Salary_Bonus_ploy();
						w.complete();
						BaseTabPanel.getInstance().addTab("花红策略", w);
					}else if(command.equals("Social_base")){
						Social_base w = new Social_base();
						w.complete();
						BaseTabPanel.getInstance().addTab("社保基数设置", w);
					}else if(command.equals("Social_S")){
						Social_S w = new Social_S();
						w.complete();
						BaseTabPanel.getInstance().addTab("社保策略", w);
					}else if(command.equals("Hr_helper")){
						Hr_helper w = new Hr_helper();
						w.complete();
						BaseTabPanel.getInstance().addTab("帮工工资", w);
					}else if(command.equals("Salary_Ploy")){
						Salary_Ploy w = new Salary_Ploy();
						w.complete();
						BaseTabPanel.getInstance().addTab("工资策略", w);
					}
				}
			}
			
		});
	}
	
	public void initWidget(){
		ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
		panel.setWidth(150);
		panel.setHeight(500);
		panel.setLayout(new AccordionLayout());  
		panel.setBodyBorder(false); 
		
		
		ContentPanel salary = new ContentPanel();
		salary.setHeading("薪金");
		salary.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE
				.menu_money()));
		salary.setLayout(new FitLayout());
		salary.add(initSalaryTreePanel());
		
		ContentPanel employ = new ContentPanel();
		employ.setHeading("雇员");
		employ.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE
						.people()));
		employ.setLayout(new FitLayout());
		employ.add(initEmpolyTreePanel());
		
		panel.add(salary);
		panel.add(employ);
		add(panel);
		
	}
	
	public void loadSalary(){
		String sql = "select * from menu where menucode like 'A003%' order by menucode asc";
		salarystore.getStoreinfo().setSql(sql);
		salarystore.getStoreinfo().setForcenolimit("true");
		salarystore.load();
	}
	
	public void loadEmploy(){
		String sql = "select * from menu where menucode like 'A005%' order by menucode asc";
		employstore.getStoreinfo().setSql(sql);
		employstore.getStoreinfo().setForcenolimit("true");
		employstore.load();
	}
	
	BaseTreeStore<TreeNodeModelData> salarystore = null;

	BaseTreeStore<TreeNodeModelData> getStoreSalary() {
		if (salarystore == null) {
			createSalaryStore();
		}
		return salarystore;
	}

	BaseTreeStore<TreeNodeModelData> createSalaryStore() {
		StoreInfo si = new StoreInfo();
		si.setTreesortcolumn("menucode");
		si.setKeyname(new String[] { "icode" });
		si.setTablename("menu");

		salarystore = new BaseTreeStore<TreeNodeModelData>(si, false);
		loadSalary();
		return salarystore;
	}
	
	BaseTreeStore<TreeNodeModelData> employstore = null;

	BaseTreeStore<TreeNodeModelData> getEmployStore() {
		if (employstore == null) {
			createEmployStore();
		}
		return employstore;
	}

	BaseTreeStore<TreeNodeModelData> createEmployStore() {
		StoreInfo si = new StoreInfo();
		si.setTreesortcolumn("menucode");
		si.setKeyname(new String[] { "icode" });
		si.setTablename("menu");

		employstore = new BaseTreeStore<TreeNodeModelData>(si, false);
		loadEmploy();
		return employstore;
	}

	public TreePanel<TreeNodeModelData> initSalaryTreePanel() {
		treeSalray = new TreePanel<TreeNodeModelData>(getStoreSalary());
		treeSalray.setDisplayProperty("menuname");
		treeSalray.setAutoLoad(true);
		treeSalray.setIconProvider(bmip);
		treeSalray.setCheckStyle(CheckCascade.NONE); 
		TreePanelSelectionModel<TreeNodeModelData> tpsm = new TreePanelSelectionModel<TreeNodeModelData>();
		tpsm.setSelectionMode(SelectionMode.SINGLE);
		treeSalray.setSelectionModel(tpsm);
		return treeSalray;
	}
	
	public TreePanel<TreeNodeModelData> initEmpolyTreePanel() {
		treeEmploy = new TreePanel<TreeNodeModelData>(getEmployStore());
		treeEmploy.setDisplayProperty("menuname");
		treeEmploy.setAutoLoad(true);
		treeEmploy.setIconProvider(bmip);
		treeEmploy.setCheckStyle(CheckCascade.NONE); 
		TreePanelSelectionModel<TreeNodeModelData> tpsm = new TreePanelSelectionModel<TreeNodeModelData>();
		tpsm.setSelectionMode(SelectionMode.SINGLE);
		treeEmploy.setSelectionModel(tpsm);
		return treeEmploy;
	}
	
	class BaseModelIconProvider<M extends ModelData> implements ModelIconProvider<M> {

		@Override
		public AbstractImagePrototype getIcon(M model) {
		
			if (model.get("menucode") != null && model.get("menucode").equals("A003"))
				return AbstractImagePrototype.create(BaseResource.INSTANCE
						.menu_money());
			else if(model.get("menucode") != null && model.get("menucode").toString().startsWith(("A00300")))
				return AbstractImagePrototype.create(BaseResource.INSTANCE
						.money_yen());
			else if (model.get("menucode") != null && model.get("menucode").equals("A005"))
				return AbstractImagePrototype.create(BaseResource.INSTANCE
						.people());
			else {
				return AbstractImagePrototype.create(BaseResource.INSTANCE
						.organization16());
				
			}
		}
	}
	
	
}
